package redis;

import lombok.extern.log4j.Log4j2;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Set;

/**
 * @author Khai Loon
 * @version 2017/1/10 0010 下午 4:57
 */
@Log4j2
public class Redis {
    private JedisPool pool;

    @Before
    public void init() {
        JedisPoolConfig config = new JedisPoolConfig();//创建redis连接池
        // 最大分配的对象数  :控制一个pool最多有多少个状态为idle的jedis实例
        config.setMaxTotal(300);
        //控制一个pool可分配多少个jedis实例，通过pool.getResource()来获取
        //如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)
        config.setMaxIdle(100);
        // 当池内没有返回对象时，最大等待时间 : 超时时间
        config.setMaxWaitMillis(100000);
        // 当调用borrow Object方法时，是否进行有效性检查: 在borrow一个jedis实例时，是否提前进行alidate操作；如果为true，则得到的jedis实例均是可用的；
        config.setTestOnBorrow(true);
        // 当调用return Object方法时，是否进行有效性检查 :在还给pool时，是否提前进行validate操作
        config.setTestOnReturn(true);
        pool = new JedisPool(config, "127.0.0.1", 6379, 200000);
    }

    @Test
    public void string() throws InterruptedException {
       /* for (int i = 20; i <= 40; i++) {//这里自己设置用多少线程并发访问
            pool.getResource().set(String.valueOf(i), UUID.randomUUID().toString());
        }
        for (int i = 20; i <= 40; i++) {
            String result = pool.getResource().get(String.valueOf(i));
            log.debug(result);
        }*/
       /* String result = pool.getResource().set("abc", "123456");

        String result1 = pool.getResource().get("abc");

        Long result2 = pool.getResource().del("abc");
*/
        pool.getResource().set("abc", "123456");
        Long l = pool.getResource().expire("abc", 5);
        String result1 = pool.getResource().get("abc");
        Thread.sleep(6000L);
        String result2 = pool.getResource().get("abc");
    }


    /**
     * 对象转字节数组
     */
    public byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        return null;
    }

    @Test
    public void list() {
        Jedis jedis = new Jedis("localhost");
        jedis.lpush("util", "1", "2", "3", "4");
        List<String> list = jedis.lrange("util", 0, 5);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("Stored string in redis:: " + list.get(i));
        }
    }

    @Test
    public void keys() {
        //连接本地的 Redis 服务
        Jedis jedis = new Jedis("localhost");
        // 获取数据并输出
        Set<String> list = jedis.keys("*");
        list.forEach(System.out::println);
    }

    @Test
    public void one() {
        String a = pool.getResource().set("likailun", "1");
        pool.close();
    }

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(6379);
        Socket socket = serverSocket.accept();
        byte[] result = new byte[2048];
        socket.getInputStream().read(result);
        System.out.println(new String(result));
    }


    @Test
    public void run() throws InterruptedException {
        Thread.sleep(5000L);
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.set("a", "123");
        jedis.close();
    }

}
